package com.example.chat.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chat.beans.Admin;
import com.example.chat.beans.PointWithdrawal;
import com.example.chat.mapper.AdminMapper;
import com.example.chat.mapper.PointWithdrawalMapper;
import com.example.chat.result.ApiResult;
import com.example.chat.result.Base;
import com.example.chat.result.ListResult;
import com.example.chat.utils.CheckUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class PointWithdrawalService extends ServiceImpl<PointWithdrawalMapper, PointWithdrawal> {

    @Resource
    CheckUtils checkUtils;
    @Resource
    AdminMapper adminMapper;

    public Integer add(Admin admin, Integer point, String zfbName, String zfbAccount) {
        PointWithdrawal pointWithdrawal = new PointWithdrawal();
        pointWithdrawal.setAdminId(admin.getId());
        pointWithdrawal.setPoint(point);
        pointWithdrawal.setMoney(point * 0.01);
        pointWithdrawal.setZfbName(zfbName);
        pointWithdrawal.setZfbAccount(zfbAccount);
        pointWithdrawal.setStatus(0);
        pointWithdrawal.setApplyTime(System.currentTimeMillis());
        baseMapper.insert(pointWithdrawal);
        return pointWithdrawal.getId();
    }

    public ListResult<List<PointWithdrawal>> withdrawalList(Integer userId, String token, Integer page, Integer limit, Integer searchStatus, String phone) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "");
        if (apiResult.getStatus() != 1)
            return new ListResult<>(apiResult.getStatus(), apiResult.getMsg(), null, 0, 0, 0);
        ListResult<List<PointWithdrawal>> result = new ListResult<>();
        int status = 0;
        String msg = "获取失败";
        try {
            int adminId = 0;
            Admin admin = adminMapper.selectById(userId);
            if (admin.getType() == 1) {
                adminId = admin.getId();
            }
            Page<PointWithdrawal> pages = new Page<>(page, limit);
            IPage<PointWithdrawal> iPages = baseMapper.selectBySearch(pages, adminId, searchStatus, phone);
            result.setData(iPages.getRecords());
            result.setPage(page);
            result.setLimit(limit);
            result.setTotal(iPages.getTotal());
            msg = "获取成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> auditWithdrawal(Integer userId, String token, Integer ids) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token, ids), userId, token, "");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "更新失败";
        try {
            PointWithdrawal withdrawal = baseMapper.selectById(ids);
            if (withdrawal != null) {
                Admin admin = adminMapper.selectById(userId);
                if (admin != null) {
                    if (admin.getType() == 2) {
                        withdrawal.setStatus(2);
                        baseMapper.updateById(withdrawal);
                        status = 1;
                        msg = "更新成功";
                    } else msg = "权限不足";
                } else msg = "用户不存在";
            } else msg = "信息不存在";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }
}
